package com.tianzeculture.stagebao.videocut;

import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.videocut.adapter.VideoSelectAdapter;
import com.tianzeculture.stagebao.videocut.models.VideoInfo;
import com.tianzeculture.stagebao.videocut.utils.TrimVideoUtil;
import com.tianzeculture.stagebao.videocut.widget.SpacesItemDecoration;

import java.util.List;

import iknow.android.utils.callback.SimpleCallback;
import iknow.android.utils.callback.SingleCallback;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Author：J.Chou
 * Date：  2016.08.01 2:23 PM
 * Email： who_know_me@163.com
 * Describe:
 */
public class VideoSelectActivity extends AppCompatActivity implements View.OnClickListener {

    private VideoSelectAdapter mVideoSelectAdapter;
    private String mVideoPath;
    RecyclerView videoSelectRecyclerview;
    ImageView videoShoot, mBtnBack;
    TextView nextStep;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.video_select_layout);

        videoSelectRecyclerview = (RecyclerView) findViewById(R.id.video_select_recyclerview);
        videoShoot = (ImageView) findViewById(R.id.video_shoot);
        mBtnBack = (ImageView) findViewById(R.id.mBtnBack);
        nextStep = (TextView) findViewById(R.id.next_step);

        GridLayoutManager manager = new GridLayoutManager(this, 4);
        videoSelectRecyclerview.addItemDecoration(new SpacesItemDecoration(5));
        videoSelectRecyclerview.setHasFixedSize(true);

        videoSelectRecyclerview.setAdapter(mVideoSelectAdapter = new VideoSelectAdapter(this));
        videoSelectRecyclerview.setLayoutManager(manager);

        videoShoot.setOnClickListener(this);
        mBtnBack.setOnClickListener(this);
        nextStep.setOnClickListener(this);

        nextStep.setTextAppearance(this, R.style.gray_text_18_style);
        nextStep.setEnabled(false);

        mVideoSelectAdapter.setItemClickCallback(new SingleCallback<Boolean, VideoInfo>() {
            @Override
            public void onSingleCallback(Boolean isSelected, VideoInfo video) {
                if (video != null) mVideoPath = video.getVideoPath();
                nextStep.setEnabled(isSelected);
                nextStep.setTextAppearance(VideoSelectActivity.this, isSelected ? R.style.blue_text_18_style : R.style.gray_text_18_style);
            }
        });

//        RxPermissions rxPermissions = new RxPermissions(this);
//    rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE).subscribe(granted -> {
//          if (granted) { // Always true pre-M
//            TrimVideoUtil.loadVideoFiles(this, new SimpleCallback() {
//              @SuppressWarnings("unchecked")
//              @Override
//              public void success(Object obj) {
//                mVideoSelectAdapter.setVideoData((List<VideoInfo>) obj);
//              }
//            });
//          } else {
//            finish();
//          }
//        });
//        rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE) //权限名称，多个权限之间逗号分隔开
//                .subscribe(new Consumer<Boolean>() {
//                    @Override
//                    public void accept(Boolean granted) throws Exception {
//                        Log.e("TAG", "{accept}granted=" + granted);//执行顺序——1【多个权限的情况，只有所有的权限均允许的情况下granted==true】
//                        if (granted) { // 在android 6.0之前会默认返回true
//                            // 已经获取权限
////                            Toast.makeText(VideoSelectActivity.this, "已经获取权限", Toast.LENGTH_SHORT).show();
////                            String deviceId = ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();//根据不同的手机设备返回IMEI，MEID或者ESN码
////                            Toast.makeText(VideoSelectActivity.this, "{accept}deviceId=" + deviceId, Toast.LENGTH_SHORT).show();
//
//                            TrimVideoUtil.loadVideoFiles(VideoSelectActivity.this, new SimpleCallback() {
//                                @SuppressWarnings("unchecked")
//                                @Override
//                                public void success(Object obj) {
//                                    mVideoSelectAdapter.setVideoData((List<VideoInfo>) obj);
//                                }
//                            });
//                        } else {
//                            // 未获取权限
//                            Toast.makeText(VideoSelectActivity.this, "您没有授权该权限，请在设置中打开授权", Toast.LENGTH_SHORT).show();
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        Log.e("TAG", "{accept}");//可能是授权异常的情况下的处理
//                    }
//                }, new Action() {
//                    @Override
//                    public void run() throws Exception {
//                        Log.e("TAG", "{run}");//执行顺序——2
//                    }
//                });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mBtnBack:
                finish();
                break;
            case R.id.next_step:
                VideoTrimmerActivity.call(VideoSelectActivity.this, mVideoPath);
                break;
            case R.id.video_shoot:
                break;
        }

    }
}
